package cn.anecansaitin.industrialization_expansion.client.model;

import cn.anecansaitin.industrialization_expansion.ModConstant;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ModelRotation;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.data.EmptyModelData;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

public class ExtendedModel {
    private static final Pattern VALID_NAME = Pattern.compile("[^a-z_]");
    private static final Map<String, IBakedModel> VALUES = new ConcurrentHashMap<>();
    public static final IBakedModel MISS = get("miss");

    @Nullable
    public static IBakedModel get(String name) {
        return VALUES.computeIfAbsent(name, k -> {
            if (VALID_NAME.matcher(name).find())
                throw new IllegalArgumentException("ToolModuleType.get() called with invalid name: " + name);

            IBakedModel bake = Objects.requireNonNull(ModelLoader.instance()).bake(new ResourceLocation(ModConstant.MOD_ID, "item/" + name), ModelRotation.X0_Y0);
            return bake == null || bake.getParticleTexture(EmptyModelData.INSTANCE).getName().getPath().equals("missingno") ? MISS : bake;
        });
    }
}
